import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler


class DataPreprocessor:
    """数据处理类"""

    def __init__(self):
        self.scaler = StandardScaler()

    def preprocess(self, x, y=None):
        """数据标准化处理, y不为空，返回元组(x,y)；y为空返回x"""
        if y is None:
            return self.scaler.transform(x)
        return self.scaler.fit_transform(x), y

    def doNothing(self, x, y=None):
        if y is None:
            return x
        return x, y


class DataLoader:
    """数据读取类"""

    def __init__(self, shuffle=False):
        self.x = None
        self.y = None
        self.shuffle = shuffle

    def load_csv(self, path):
        """读取CSV文件"""
        df = pd.read_csv(path)
        row, col = df.shape
        self.x = df.iloc[:, :col - 1].values
        self.y = df.iloc[:, col - 1].values
        if self.shuffle:
            indices = np.random.permutation(row)
            self.x = self.x[indices]
            self.y = self.y[indices]


if __name__ == "__main__":
    data = DataLoader(shuffle=True)
    data.load_csv(r"E:\WyLab\GfGroup\code\WyLab\Classify\sample.csv")
    print(data.x, data.y)
